package com.example.mytestdemo.view.view

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import com.example.mytestdemo.R

/**
 * @author tgw
 * @date 2021/7/8
 * @describe
 */
/**
 * description:自定义dialog
 */
class CustomDialog(private val mContext: Context) : Dialog(
    mContext, R.style.CustomDialog) {
    private val widthScale = 0.3

    /**
     * 显示的图片
     */
    private var imageIv: ImageView? = null

    /**
     * 显示的标题
     */
    private var titleTv: TextView? = null

    /**
     * 显示的消息
     */
    private var messageTv: TextView? = null

    /**
     * 确认和取消按钮
     */
    private var negtiveBn: Button? = null
    private var positiveBn: Button? = null

    /**
     * 按钮之间的分割线
     */
    private var columnLineView: View? = null

    /**
     * 都是内容数据
     */
    var message: String? = null
        private set
    var title: String? = null
        private set
    var positive: String? = null
        private set
    var negtive: String? = null
        private set
    var imageResId = -1 //默认不显示图标
        private set

    /**
     * 底部是否只有一个按钮
     */
    var isSingle = false
        private set

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.common_dialog_layout)
        // 实例化新的窗口
        val w = window
        // 获取默认显示数据
//        Display display = w.getWindowManager().getDefaultDisplay();
//        // 获取窗口的背景图片
//        Resources resources = mContext.getResources();
//
//        // 窗口的标题为空
//        w.setTitle(null);
//        // 定义窗口的宽和高
//        int width = (int) (display.getWidth() * 0.4);
//        int height = (int) (display.getHeight() * 0.6);
//        // 设置窗口的大小
//        w.setLayout(width, height);
//        // 设置窗口的显示位置
//        w.setGravity(Gravity.CENTER);
//        // 设置窗口的属性
//        WindowManager.LayoutParams wl = w.getAttributes();
//        w.setAttributes(wl);
        val lp = w!!.attributes
        val d = mContext.resources.displayMetrics //获取屏幕的宽和高用
        lp.width = (d.widthPixels * widthScale).toInt()
        //        lp.height =(int) (d.heightPixels*0.4);
        w.setGravity(Gravity.CENTER)
        w.attributes = lp
        // 获取控件
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false)
        //初始化界面控件
        initView()
        //初始化界面数据
        refreshView()
        //初始化界面控件的事件
        initEvent()
    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private fun initEvent() {
        //设置确定按钮被点击后，向外界提供监听
        positiveBn!!.setOnClickListener { v ->
            dismiss()
            if (onClickBottomListener != null) {
                onClickBottomListener!!.onPositiveClick(v)
            }
        }
        //设置取消按钮被点击后，向外界提供监听
        negtiveBn!!.setOnClickListener { v ->
            dismiss()
            if (onClickBottomListener != null) {
                onClickBottomListener!!.onNegtiveClick(v)
            }
        }
    }

    /**
     * 初始化界面控件的显示数据
     */
    private fun refreshView() {
        //如果用户自定了title和message
        if (!TextUtils.isEmpty(title)) {
            titleTv!!.text = title
            titleTv!!.visibility = View.VISIBLE
        } else {
            titleTv!!.visibility = View.GONE
        }
        if (!TextUtils.isEmpty(message)) {
            messageTv!!.text = message
        }
        //如果设置按钮的文字
        if (!TextUtils.isEmpty(positive)) {
            positiveBn!!.text = positive
        } else {
            positiveBn!!.text = "确定"
        }
        if (!TextUtils.isEmpty(negtive)) {
            negtiveBn!!.text = negtive
        } else {
            negtiveBn!!.text = "取消"
        }
        if (imageResId != -1) {
            imageIv!!.setImageResource(imageResId)
            imageIv!!.visibility = View.VISIBLE
        } else {
            imageIv!!.visibility = View.GONE
        }
        /**
         * 只显示一个按钮的时候隐藏取消按钮，回掉只执行确定的事件
         */
        if (isSingle) {
            columnLineView!!.visibility = View.GONE
            negtiveBn!!.visibility = View.GONE
        } else {
            negtiveBn!!.visibility = View.VISIBLE
            columnLineView!!.visibility = View.VISIBLE
        }
    }

    override fun show() {
        super.show()
        refreshView()
    }

    /**
     * 初始化界面控件
     */
    private fun initView() {
        negtiveBn = findViewById<View>(R.id.negtive) as Button
        positiveBn = findViewById<View>(R.id.positive) as Button
        titleTv = findViewById<View>(R.id.title) as TextView
        messageTv = findViewById<View>(R.id.message) as TextView
        imageIv = findViewById<View>(R.id.image) as ImageView
        columnLineView = findViewById(R.id.column_line)
    }

    /**
     * 设置确定取消按钮的回调
     */
    var onClickBottomListener: OnClickBottomListener? = null
    fun setOnClickBottomListener(onClickBottomListener: OnClickBottomListener?): CustomDialog {
        this.onClickBottomListener = onClickBottomListener
        return this
    }

    interface OnClickBottomListener {
        /**
         * 点击确定按钮事件
         */
        fun onPositiveClick(view: View)

        /**
         * 点击取消按钮事件
         */
        fun onNegtiveClick(view: View) 
    }

    fun setMessage(message: String?): CustomDialog {
        this.message = message
        return this
    }

    fun setTitle(title: String?): CustomDialog {
        this.title = title
        return this
    }

    fun setPositive(positive: String?): CustomDialog {
        this.positive = positive
        return this
    }

    fun setNegtive(negtive: String?): CustomDialog {
        this.negtive = negtive
        return this
    }

    fun setSingle(single: Boolean): CustomDialog {
        isSingle = single
        return this
    }

    /**
     * 设置图片
     */
    fun setImageResId(imageResId: Int): CustomDialog {
        this.imageResId = imageResId
        return this
    }

    /**
     * 设置标识
     */
    fun setViewTag(tag: Int) {
        positiveBn!!.tag = tag
        negtiveBn!!.tag = tag
    }

}